package com.xmkj.bearbiz.member;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.common.BearBean.VipUserInfo;
import com.common.BearMethods.GetVipUserDatasMethods;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.xmkj.bearbiz.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.functions.Action1;

@SuppressLint("ValidFragment")
public class LifeStyleFragment extends BaseMvpFragment {
    @BindView(R.id.tv_edu)
    TextView tvEdu;
    @BindView(R.id.iv_go)
    ImageView ivGo;
    @BindView(R.id.rl_edu)
    RelativeLayout rlEdu;
    @BindView(R.id.tv_language)
    TextView tvLanguage;
    @BindView(R.id.iv_goo)
    ImageView ivGoo;
    @BindView(R.id.rl_language)
    RelativeLayout rlLanguage;
    @BindView(R.id.tv_vehicle)
    TextView tvVehicle;
    @BindView(R.id.iv_gooo)
    ImageView ivGooo;
    @BindView(R.id.rl_vehicle)
    RelativeLayout rlVehicle;
    @BindView(R.id.et_topic)
    EditText etTopic;
    @BindView(R.id.et_unlike_topic)
    EditText etUnlikeTopic;
    @BindView(R.id.rb_yes)
    RadioButton rbYes;
    @BindView(R.id.rb_no)
    RadioButton rbNo;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.et_eat_like)
    EditText etEatLike;
    @BindView(R.id.tv_home_info)
    TextView tvHomeInfo;
    @BindView(R.id.iv_goooo)
    ImageView ivGoooo;
    @BindView(R.id.rl_home_info)
    RelativeLayout rlHomeInfo;

    Unbinder unbinder;
    private String deptcode;
    private String vipcardid;
    private VipUserInfo bean;
    private int smoke;
    private OptionsPickerView eduPicker;
    private OptionsPickerView lanuagePicker;
    private OptionsPickerView vihicalPicker;
    private OptionsPickerView homePicker;
    private SaveUserInfoBean submitBean = new SaveUserInfoBean();

    @SuppressLint("ValidFragment")
    public LifeStyleFragment(String deptcode, String vipcardid) {

        this.deptcode = deptcode;
        this.vipcardid = vipcardid;
    }

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_life_style;
    }

    @Override
    protected void onViewCreated(View view) {

    }

    @Override
    protected void doLogicFunc() {

        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.SAVE_USER_INFO) && (Boolean) rxKeyEvent.getValue()) {
                    if(EmptyUtils.isEmpty(bean)){
                        submitBean.vipcardid = "";
                        submitBean.deptcode = "";
                    }else {
                        submitBean.vipcardid = bean.vipcardid;
                        submitBean.deptcode = bean.deptcode;
                    }
                    submitBean.education = getEduSubmit(getTextViewString(tvEdu));
                    submitBean.vehicle  = getViHicalSubmit(getTextViewString(tvVehicle));
                    submitBean.language  = getSumitLanguage(getTextViewString(tvLanguage));
                    submitBean.like_topic = getEditTextStr(etTopic);
                    submitBean.taboo_topic = getEditTextStr(etUnlikeTopic);
                    submitBean.like_smoke = smoke+"";
                    submitBean.food_preference = getEditTextStr(etEatLike);
                    submitBean.family_information = getSubmitHomeInfo(getTextViewString(tvHomeInfo));
                    RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.SAVE_FROM_LIFE_STYLE, submitBean));




                }
            }
        }));
        getVipUserData();
        // 教育程度Picker
        initEduPicker();
        // 语言Picker
        initlanguagePicker();

        // 交通工具Picker
        initVihicalPicker();
        // 家庭信息Picker
        initHomePicker();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.rb_yes:

                        smoke = 1;
                        break;
                    case R.id.rb_no:
                        smoke = 2;
                        break;

                }
            }
        });
        attachClickListener(rlEdu);
        attachClickListener(rlHomeInfo);
        attachClickListener(rlLanguage);
        attachClickListener(rlVehicle);
    }

    private String getSumitLanguage(String langue) {
        if (EmptyUtils.isEmpty(langue)) {
            return "";
        } else if (langue.contains("普通话")) {
            return "1";
        } else if (langue.contains("方言")) {
            return "2";
        } else if (langue.contains("英语")) {
            return "3";
        }else if (langue.contains("其他小语种")) {
            return "4";
        }

        return langue;
    }

    private String getSubmitHomeInfo(String homeinfo) {
        if (EmptyUtils.isEmpty(homeinfo)) {
            return "";
        } else if (homeinfo.contains("单身")) {
            return "1";
        } else if (homeinfo.contains("已婚无孩子")) {
            return "2";
        } else if (homeinfo.contains("已婚有孩子")) {
            return "3";
        }
        return homeinfo;
    }

    private String getViHicalSubmit(String vihical) {
        if (EmptyUtils.isEmpty(vihical)) {
            return "";
        } else if (vihical.contains("步行")) {
            return "1";
        } else if (vihical.contains("自行车")) {
            return "2";
        } else if (vihical.contains("摩托车")) {
            return "3";
        } else if (vihical.contains("轿车")) {
            return "4";
        }
        return vihical;
    }

    private String getEduSubmit(String edu) {
        if (EmptyUtils.isEmpty(edu)) {
            return "";
        } else if (edu.contains("初中以下")) {
            return "1";
        } else if (edu.contains("高中")) {
            return "2";
        } else if (edu.contains("大专")) {
            return "3";
        } else if (edu.contains("本科")) {
            return "4";
        } else if (edu.contains("研究生")) {
            return "5";
        }
        return edu;
    }

    @Override
    protected void onViewClicked(View view) {
        if (rlEdu.getId() == view.getId()) {
            if (eduPicker != null) {
                eduPicker.show();
            }
        } else if (rlHomeInfo.getId() == view.getId()) {
            if (homePicker != null) {
                homePicker.show();
            }
        } else if (rlLanguage.getId() == view.getId()) {
            if (lanuagePicker != null) {
                lanuagePicker.show();
            }
        } else if (rlVehicle.getId() == view.getId()) {
            if (vihicalPicker != null) {
                vihicalPicker.show();
            }
        }
    }

    private void getVipUserData() {
        CommonSubscriber<VipUserInfo> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                bean = (VipUserInfo) o;
                tvEdu.setText(bean.education);
                tvLanguage.setText(bean.language);
                tvVehicle.setText(bean.vehicle);
                etTopic.setText(bean.like_topic);
                etUnlikeTopic.setText(bean.taboo_topic);
                if (bean.like_smoke == 1) {
                    // 喜欢抽烟
                    radioGroup.check(rbYes.getId());
                } else {
                    radioGroup.check(rbNo.getId());
                }

                smoke = bean.like_smoke;
                etEatLike.setText(bean.food_preference);
                tvHomeInfo.setText(bean.family_information);



            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        GetVipUserDatasMethods.getInstance().seeVipUser(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID), vipcardid, deptcode);
        rxManager.add(subscriber);
    }

    private void initHomePicker() {
        final List<String> homeList = new ArrayList<>();
        homeList.add("单身");
        homeList.add("已婚无孩子");
        homeList.add("已婚有孩子");

        homePicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

//                shopCode = shopBeans.get(options1).lists.get(options2).clientcode;
//                tvCondition.setText(shopBeans.get(options1).lists.get(options2).clientname);
//                typecode = shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeCode;
//                tvGoodsSort.setText(one.get(options1) + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).TypeName + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeName);
            /*        tvProductYear.setText(years.get(options1));
                    year = years.get(options1);*/

//                tvTip.setText(bean.customer_identity_ids.get(options1));
//                tvLanguage.setText(lanuageList.get(options1));
                tvHomeInfo.setText(homeList.get(options1));
            }
        }).setSubmitText(getString(R.string.insure)).
                setCancelText(getString(R.string.cacal)).
                build();
        homePicker.setPicker(homeList);
    }

    private void initVihicalPicker() {
        final List<String> VihicalList = new ArrayList<>();
        VihicalList.add("步行");
        VihicalList.add("自行车");
        VihicalList.add("摩托车");
        VihicalList.add("轿车");

        vihicalPicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

//                shopCode = shopBeans.get(options1).lists.get(options2).clientcode;
//                tvCondition.setText(shopBeans.get(options1).lists.get(options2).clientname);
//                typecode = shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeCode;
//                tvGoodsSort.setText(one.get(options1) + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).TypeName + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeName);
            /*        tvProductYear.setText(years.get(options1));
                    year = years.get(options1);*/

//                tvTip.setText(bean.customer_identity_ids.get(options1));
//                tvLanguage.setText(lanuageList.get(options1));
                tvVehicle.setText(VihicalList.get(options1));
            }
        }).setSubmitText(getString(R.string.insure)).
                setCancelText(getString(R.string.cacal)).
                build();
        vihicalPicker.setPicker(VihicalList);
    }

    private void initlanguagePicker() {
        final List<String> lanuageList = new ArrayList<>();
        lanuageList.add("普通话");
        lanuageList.add("方言");
        lanuageList.add("英语");
        lanuageList.add("其他小语种");

        lanuagePicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

//                shopCode = shopBeans.get(options1).lists.get(options2).clientcode;
//                tvCondition.setText(shopBeans.get(options1).lists.get(options2).clientname);
//                typecode = shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeCode;
//                tvGoodsSort.setText(one.get(options1) + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).TypeName + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeName);
            /*        tvProductYear.setText(years.get(options1));
                    year = years.get(options1);*/

//                tvTip.setText(bean.customer_identity_ids.get(options1));
                tvLanguage.setText(lanuageList.get(options1));

            }
        }).setSubmitText(getString(R.string.insure)).
                setCancelText(getString(R.string.cacal)).
                build();
        lanuagePicker.setPicker(lanuageList);
    }

    private void initEduPicker() {
        final List<String> eduList = new ArrayList<>();
        eduList.add("初中以下");
        eduList.add("高中/中专");
        eduList.add("大专");
        eduList.add("本科");
        eduList.add("研究生以上");

        eduPicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

//                shopCode = shopBeans.get(options1).lists.get(options2).clientcode;
//                tvCondition.setText(shopBeans.get(options1).lists.get(options2).clientname);
//                typecode = shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeCode;
//                tvGoodsSort.setText(one.get(options1) + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).TypeName + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeName);
            /*        tvProductYear.setText(years.get(options1));
                    year = years.get(options1);*/

//                tvTip.setText(bean.customer_identity_ids.get(options1));
                tvEdu.setText(eduList.get(options1));

            }
        }).setSubmitText(getString(R.string.insure)).
                setCancelText(getString(R.string.cacal)).
                build();
        eduPicker.setPicker(eduList);
    }

    @Override
    public void bindKnife() {
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    protected void setStatusBar() {
        super.setStatusBar();
        m_statusBar.setVisibility(View.GONE);
    }
}
